package com.financeiro.util.jpa;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.context.RequestScoped;
import javax.enterprise.inject.Disposes;
import javax.enterprise.inject.Produces;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/*ApplicationScoped cria a classe no scopo da aplicação, cria um para aplicação inteira*/
@ApplicationScoped
public class EntityManagerProducer {

    private EntityManagerFactory factory;

    public EntityManagerProducer() {
        this.factory = Persistence.createEntityManagerFactory("financeiroPU");
    }

    /* RequestScoped cria o EntityManager em cada requisição*/
    @Produces
    @RequestScoped
    public EntityManager create() {
        return factory.createEntityManager();
    }
    
    /* fecha o EntityManager de cada requisição*/
    public void close(@Disposes EntityManager menager) {
        menager.close();;
    }
}
